﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Antioch.Base
{
	[Jemgine.Engine.Category("Antioch System"), Jemgine.Engine.Component]
	public class Health : Jemgine.Engine.EntityComponent
	{
        public int MaxHealth { get; set; }
        public int CurrentHealth { get; set; }

        [ContentSerializer(Optional = true)]
        public bool Vulnerable { get; set; }

        public Health()
        {
            AddOutput("OnDeath", null);
            AddOutput("OnHurt", null);

            Vulnerable = true;
        }

        public virtual void TakeDamage(int Damage)
        {
            if (Vulnerable)
            {
                CurrentHealth -= Damage;
                OutputTerminals[1].Fire(Jemgine.Data.ScriptPayload.CreatePayload(Me));
                if (CurrentHealth <= 0) 
                    OutputTerminals[0].Fire(Jemgine.Data.ScriptPayload.CreatePayload(Me));
            }
        }		
	}

    [Jemgine.Engine.Node, Jemgine.Engine.Category("Antioch System")]
    public class GiveDamage : Jemgine.Data.ScriptNode
    {
        public int Damage { get; set; }

        public GiveDamage()
        {
            AddInput("Fire", null);
            AddReference("To", null);
        }

        public override void FireTerminal(int InputID, Jemgine.Data.ScriptPayload Payload)
        {
            Jemgine.Data.EntityRecord Entity = ReferenceTerminals[0].GetTarget(Payload) as Jemgine.Data.EntityRecord;
            if (Entity == null) return;
            Health Health = Entity.GetComponent<Health>();
            if (Health != null) Health.TakeDamage(Damage);
        }
    }
}
